function isEmptyNumber(input) {
    return (input === null || input === undefined || (typeof input === 'string' && !input.trim()))
}
// 获取单位长度
function getUnitLen(unit, reverseUnit) {
    if (isEmptyNumber(unit)) {
        return 0
    }
    if (unit < 1) { // 0.01
        return (String(unit).split('.')[1].length) * (-1) * (reverseUnit === true ? -1 : 1)
    }
    if (unit > 1) {
        return (String(unit).trim().split('').length - 1) * (reverseUnit === true ? -1 : 1)
    }
}

/**
 * 
 * @param {*} v  待转数字
 * @param {*} unit 单位
 * @param {*} reverseUnit 转换方向（下面例子）
 * @param {*} decimal 小数位个数
 * @returns 
 */
function numberFormater(v, unit, reverseUnit, decimal) {
    v = v + ''
    let dotPos = v.indexOf('.')
    dotPos = dotPos > -1 ? dotPos : v.length
    v = v.split('')
    /**
     * unit > 0 向 左移位
     * unit < 0 向 右移位
     */
    unit = getUnitLen(unit, reverseUnit)
    const ilen = v.slice(0, dotPos).length
    const flen = v.slice(dotPos + 1).length
    const _obit = Array.from({ length: 10 }).map(() => '0')
    let _temp = _obit.concat(v, (dotPos === v.length ? ['.'] : []), _obit)
    dotPos = _obit.length + dotPos

    if (unit > 0) {
        _temp.splice(dotPos - unit, 0, '_dot_')
    } else {
        _temp.splice(dotPos - unit, 0, '_dot_')
    }

    _temp = _temp.map((r, index) => {
        if (r === '.') {
            return '_dot_'
        }
        if (r === '_dot_') {
            return '.'
        }
        return r
    }).filter(r => r !== '_dot_')

    // 清除 前 0
    let k = true
    while (k) {
        if (_temp.indexOf('0') === 0 && _temp[1] !== '.') {
            _temp.splice(0, 1)
        } else {
            k = false
        }
    }
    //如果存在精度
    if (decimal > 0) {
        _temp = _temp.slice(0, _temp.indexOf('.') + 1 + decimal)
    } else {
        k = true
        while (k) {
            if (_temp.lastIndexOf('0') === _temp.length - 1) {
                _temp.splice(_temp.length - 1, 1)
            } else {
                k = false
            }
        }

        if (_temp[_temp.length - 1] === '.') {
            _temp.splice(_temp.length - 1, 1)
        }
    }

    return _temp.join('')
}
/**
 * 数字格式化api
 * 精度2位：numberFormater(1234, null, false, 2) => 1234.00
 * 转为万,且精度两位 numberFormater(12345, 10000, false, 2) =>1.23
 * 转为%, 且精度两位 numberFormater(1.2345, 0.01, false, 2) =>123.45
 * 转为%, 且精度两位 numberFormater(123.45, 0.01, true, 2) =>1.23
 */
export default numberFormater